#ifndef H_RCGD_LED_H
#define H_RCGD_LED_H

/*---------------------------------------------------------------------*/
/*                     LED programming                                 */
/*---------------------------------------------------------------------*/


extern char	beep_pps_flag;
extern char	beep_alarm_flag;
extern int	beep_pps_counter, beep_alarm_counter;

extern char led_board_fail_flag;

extern int led_sync_retain_cnt;

extern void Init_LED(int fd);
extern void DisplayLED();

extern void LEDonResetOperation();



extern unsigned  ant_open_time;
extern unsigned  ant_short_time;



typedef struct PowerSensing{
	float p5v; 
	float n8v;
	float p8v;
	float threshold;
	int cnt;
	int p5v_cnt_fault;
	int n12v_cnt_fault;
	int p12v_cnt_fault; 
}PowerSensing_ ;
extern PowerSensing_ ps;

#define HOLDOVER24_NO 0
#define HOLDOVER24_NOT_YET 1
#define HOLDOVER24_YES 2

extern unsigned ant_open_time;
extern unsigned ant_short_time;
extern unsigned no_satellite_time;
extern unsigned ant_ok_logged;
extern unsigned int holdover_time;
extern char holdover_time_after24H ;

extern char over_1us;
extern int eb_10min_start;

void eb_10min_off(void);
void eb_40min_off(void);

#define IsAntennaFaultLimit(x) (\
		( (x) >= M_THRESHOLD_ANT_DETECT 		&& GC.RunningTime >= M_THRESHOLD_ANT_DETECT ) || \
		( (x) >= M_THRESHOLD_ANT_DETECT_SHORT 	&& GC.RunningTime <  M_THRESHOLD_ANT_DETECT )  )

////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////

#define QueryRun()		( SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION )
#define QueryOCXOHeatUp()	( (CONTROL.CTRLMODE == CTRL_OCXO_HEATUP_NOT_READY) )

#define QueryActive()					( (SYSTEM.OPERATION.STATUS == STATUS_ACTIVE) && (SYSTEM.OPERATION.LOCKMODE == LOCKED) )
#define QueryActive_Warmup()	((SYSTEM.OPERATION.OPMODE != OPMODE_OPERATION) || ((SYSTEM.OPERATION.STATUS != STATUS_ACTIVE) && (CONTROL.CTRLMODE == CTRL_OCXO_HEATUP_READY)))

#define QueryActive_HoldoverWithin24H()				\
	( ( (SYSTEM.OPERATION.STATUS == STATUS_ACTIVE) && (holdover_time_after24H == HOLDOVER24_NOT_YET) ) \
			|| frc_error.holdover == 1 )
#define QueryActive_HoldoverAfter24H()				\
	(( (SYSTEM.OPERATION.STATUS == STATUS_ACTIVE) && (holdover_time_after24H == HOLDOVER24_YES) )\
||frc_error.holdover == 2)
#define QueryActive_Recovery()	( over_1us )


#define QueryCLK_Heatup_Done()																			\
	((SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION)	&& (STATUS_Warmup==OFF) && 							\
	(led_sync_retain_cnt==0) )

#define QueryCLK_Lock()																			\
	( (( SYSTEM.OPERATION.STATUS == STATUS_ACTIVE) || 											\
	((SYSTEM.OPERATION.STATUS ==  STATUS_STANDBY) && (LOCKEDtoMASTER_SPEC_IN == OK))) &&		\
	(led_sync_retain_cnt==0) )

#define QueryCLK_Unlock()																						\
	( (((SYSTEM.OPERATION.STATUS ==  STATUS_STANDBY) || (SYSTEM.OPERATION.STATUS == STATUS_PRE_STANDBY)) && 	\
	(LOCKEDtoMASTER_SPEC_IN == FAIL)) &&																		\
	(led_sync_retain_cnt==0) )

#define QueryLock()		( ( SYSTEM.OPERATION.STATUS == STATUS_ACTIVE || SYSTEM.OPERATION.STATUS ==  STATUS_STANDBY) && SYSTEM.OPERATION.LOCKMODE == LOCKED )


#define QueryTracking()	( SYSTEM.OPERATION.STATUS != STATUS_ACTIVE && SYSTEM.OPERATION.STATUS !=  STATUS_STANDBY &&  SYSTEM.OPERATION.LOCKMODE != HOLDOVER &&  REFERENCE_MODE == EB_STATUS_RUN && SYSTEM.EB.ANTENNA == OK)

#define QueryGPSGood()		( SYSTEM.EB.ANTENNA == OK )

#define QueryRcvAlmanac()	( (SYSTEM.EB.STATUS != EB_STATUS_RUN) )
#define QueryGPSAntOpen()	( (ant_open_time == 60) )
#define QueryGPSAntShort()	( (ant_short_time == 60) )
#define QueryGPSAntNoSat()	( (SYSTEM.EB.ANTENNA == OK) && ((SYSTEM.TRACK.VISIBLE == 0) || (SYSTEM.TRACK.TRACKED == 0)) && (no_satellite_time == 100) )

#define QueryGPSBad()	(DIAG.TFOM > 4)

#define QueryFailAlarm()	( DIAG.OCXO == FAIL )

#define QueryFanAlarm()		( QueryFANALM1() == 1 || QueryFANALM2() == 1 )

#define M_THRESHOLD_ANT_DETECT			(alm_range.ant_fault)//3600
#define M_THRESHOLD_ANT_DETECT_SHORT	5


#endif

